package com.kpmg.datalake.common.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kpmg.datalake.common.dao.ProjectRptSqlMapper;
import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.enums.UseOrDelEnum;
import com.kpmg.datalake.common.exception.BusinessException;
import com.kpmg.datalake.common.model.ProjectRptSql;
import com.kpmg.datalake.common.service.IProjectRptSqlService;
import com.kpmg.datalake.common.utils.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 项目报表sql 服务实现类
 * </p>
 *
 * @author eyu7
 * @since 2018-08-16
 */
@Service
public class ProjectRptSqlServiceImpl extends ServiceImpl<ProjectRptSqlMapper, ProjectRptSql> implements IProjectRptSqlService {

    /**
     * 查询sql
     *
     * @param projectId  项目ID
     * @param rptType    报表类型
     * @param rptExpType 文件导出类型
     * @return 执行sql
     */
    @Override
    public String getSqlTextByInfo(String projectId, String rptType, String rptExpType) {
        ProjectRptSql projectRptSql = new ProjectRptSql();
        AssertUtil.assertNotEmpty(projectId, "项目ID不能为空");
        AssertUtil.assertNotEmpty(rptType, "报表类型不能为空");
        projectRptSql.setPrjId(projectId);
        projectRptSql.setRptType(rptType);
        projectRptSql.setRptExpType(rptExpType);
        projectRptSql.setDelInd(UseOrDelEnum.USE.getIntCode());
        projectRptSql = super.selectOne(new EntityWrapper<>(projectRptSql));
        if (projectRptSql == null) {
            throw new BusinessException(ResultCodeEnum.CODE4000, "未找到对应的sql");
        }
        return projectRptSql.getSqlTxt();
    }

    /**
     * 查询sql
     *
     * @param projectId 项目ID
     * @param rptType   报表类型
     * @return 执行sql
     */
    @Override
    public String getSqlTextByInfo(String projectId, String rptType) {
        return getSqlTextByInfo(projectId, rptType, null);
    }

    /**
     * 查询项目sql信息
     *
     * @param projectId  项目ID
     * @param rptType    报表类型
     * @param rptExpType 文件导出类型
     * @return ProjectRptSql
     */
    @Override
    public ProjectRptSql getProjectRptSqlByInfo(String projectId, String rptType, String rptExpType) {
        ProjectRptSql projectRptSql = new ProjectRptSql();
        AssertUtil.assertNotEmpty(rptType, "报表类型不能为空");
        AssertUtil.assertNotEmpty(projectId, "项目ID不能为空");

        projectRptSql.setRptExpType(rptExpType);
        projectRptSql.setRptType(rptType);
        projectRptSql.setPrjId(projectId);
        projectRptSql.setDelInd(UseOrDelEnum.USE.getIntCode());
        projectRptSql = super.selectOne(new EntityWrapper<>(projectRptSql));

        if (StringUtils.isEmpty(projectRptSql)) {
            throw new BusinessException(ResultCodeEnum.CODE4000, "未找到对应的sql");
        }
        return projectRptSql;
    }

    /**
     * 查询项目sql信息
     *
     * @param projectId 项目ID
     * @param rptType   报表类型
     * @return ProjectRptSql
     */
    @Override
    public ProjectRptSql getProjectRptSqlByInfo(String projectId, String rptType) {
        return getProjectRptSqlByInfo(projectId, rptType, null);
    }

}
